package com.android.personal.kotlin.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.text.format.DateFormat;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

/**
 * Intent的工具类
 * @author junjun
 */
public class IntentsUtils {

	//开始执行
	public static final int STARTDEAL = 0x01;
	//结束执行
	public static final int ENDDEAL = 0x02;

	static final int DELETEIMAGE = 0x03;

	static final int DELETEVIDEO = 0x04;

	static String imageOrVideoPath = null;

	/**调用系统有的相机*/
	public static void openCamere(Activity activity,String imagePath) {

		Uri u=Uri.fromFile(new File(imagePath));
		Intent intent = new Intent();
		intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
		intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//		activity.startActivityForResult(intent, Constants.OPEN_CAMERA);
	}
	
	/**调用系统有的图库**/
	public static void openPicture(Activity activity) {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
//		activity.startActivityForResult(intent, Constants.OPEN_PHOTO);
	}
	/**调用系统录像机*/
	public static void openVideo(Activity activity) {

//		String ss = FileUtils.getSDPATH() + DateFormat.format("yyyyMMdd_hhmmss",
//				Calendar.getInstance(Locale.CHINA)) + ".mp4";
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, "ss");
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60);// 以秒为单位
//		intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 1024 * 1024);// 以字节为单位
//		intent.putExtra(MediaStore.EXTRA_FULL_SCREEN, true);// 以字节为单位
//		intent.putExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true);// 默认值为true,这意味着自动退出电影播放器活动电影完成后玩。
//		activity.startActivityForResult(intent, Constants.OPEN_VIDEO);
	}

	/**
	 * 调取系统录像后,通过intent拿到视频路径
	 * @return
     */
	public  static String getVideoPath(Intent data, FragmentActivity activity){
		imageOrVideoPath = null;
		if(data != null && data.getData() != null){
			Uri uri = data.getData();
			Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
			if (cursor != null && cursor.moveToNext()) {
//			int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.VideoColumns._ID));
				imageOrVideoPath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA));
			}
			cursor.close();
		}
		return imageOrVideoPath;
	}

	/**
	 * 调用系统浏览图片功能
	 * 备注:如果是网络图片,必须是全路径(暂不支持网络图片路径)
	 * @param imagePath 图片路径
     */
	public static void lookImage(String imagePath,FragmentActivity activity){
		if(TextUtils.isEmpty(imagePath)){
//				CommonUtils.showMessage("图片不存在",activity);
		}else{
//				Intent intent = new Intent(activity, LookImageAc.class);
//				intent.putExtra(LookImageAc.ONEIMAGE,imagePath);
//				activity.startActivityForResult(intent,0x12);
//				intent.setAction(android.content.Intent.ACTION_VIEW);
//				File file = new File(imagePath);
//				intent.setDataAndType(Uri.fromFile(file),
//						"image/*");
//				activity.startActivity(intent);
		}
	}
	/**
	 * 调用系统视频播放功能
	 * 备注:如果是网络视频,必须是全路径(暂不支持网络图片路径)
	 * @param videoPath 图片路径
     */
	public static void lookVideo(String videoPath,FragmentActivity activity){
		if(TextUtils.isEmpty(videoPath)){
//				CommonUtils.showMessage("视频不存在",activity);
		}else{
			Intent intent = new Intent(Intent.ACTION_VIEW);
			String type = "video/" + videoPath.substring(videoPath.lastIndexOf(".") + 1);
			Uri uri;
			if(videoPath.startsWith("http")){
				 uri = Uri.parse(videoPath);
			}else{
				 uri = Uri.parse("file:///" + videoPath);
			}
			intent.setDataAndType(uri, type);
			activity.startActivity(intent);
		}
	}

	/**
	 * 删除所有视频和图片
	 * @param activity
	 * @param handle
     */
	public static void deletePhotoAndVideo(final FragmentActivity activity, final Handler handle){
		if(activity == null){
			return;
		}
		deleteDbImage(activity,handle);
		deleteDbVideo(activity,handle);
	}

	/**
	 * 删除所有图片
	 * @param activity
	 * @param handle
     */
	public static void deleteDbImage(final FragmentActivity activity, final Handler handle){
		deleleDb(activity,handle,DELETEIMAGE);
	}

	/**
	 * 删除视频
	 * @param activity
	 * @param handle
     */
	public static void deleteDbVideo(final FragmentActivity activity, final Handler handle){
		deleleDb(activity,handle,DELETEVIDEO);
	}

	/**
	 * 删除数据库资源文件
	 * @param activity
	 * @param handle
	 * @param mode
     */
	static void deleleDb(final FragmentActivity activity, final Handler handle, final int mode){
		activity.getSupportLoaderManager().initLoader(mode, null, new LoaderManager.LoaderCallbacks<Cursor>() {

			final String[] IMAGE_PROJECTION = {
					MediaStore.Images.Media.DATA,
					MediaStore.Images.Media.DISPLAY_NAME,
					MediaStore.Images.Media.DATE_ADDED,
					MediaStore.Images.Media._ID };

			final String[] VIDEO_PROJECTION = {
					MediaStore.Video.Media.DATA,
					MediaStore.Video.Media.DISPLAY_NAME,
					MediaStore.Video.Media.DATE_ADDED,
					MediaStore.Video.Media._ID };

			//当前年月日的时间戳
//			long cuuTime =  DateUtils.date2TimeMillis(DateUtils.getCuurTime(DateUtils.dateFormatYMD));

			@Override
			public Loader<Cursor> onCreateLoader(int id, Bundle args) {
				if(handle != null){
					handle.removeMessages(STARTDEAL);
					handle.sendEmptyMessage(STARTDEAL);
				}
				if(id == DELETEIMAGE) {
					CursorLoader cursorLoader = new CursorLoader(activity,
							MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_PROJECTION,
							null, null, IMAGE_PROJECTION[2] + " DESC");
					return cursorLoader;
				}else if(id == DELETEVIDEO){
					CursorLoader cursorLoader = new CursorLoader(activity,
							MediaStore.Video.Media.EXTERNAL_CONTENT_URI, VIDEO_PROJECTION,
							null, null, VIDEO_PROJECTION[2] + " DESC");
					return cursorLoader;
				}
				return null;
			}

			@Override
			public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
				if(data != null){
					int count = data.getCount();

					String path = null;
					String name = null;
					long dateTime = 0;

					if(count > 0){
						data.moveToFirst();
						do{
							if(mode == DELETEIMAGE) {
								path = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[0]));
								name = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[1]));
								dateTime = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[2]));
							}else if(mode == DELETEVIDEO){
								path = data.getString(data.getColumnIndexOrThrow(VIDEO_PROJECTION[0]));
								name = data.getString(data.getColumnIndexOrThrow(VIDEO_PROJECTION[1]));
								dateTime = data.getLong(data.getColumnIndexOrThrow(VIDEO_PROJECTION[2]));
							}

							//今天之前的图片需要删除
//							if(cuuTime > dateTime && dateTime > 0){
//								if(mode == DELETEIMAGE) {
//									deletDbImage(activity,path);
//								}else if(mode == DELETEVIDEO){
//									deletDbVideo(activity,path);
//								}
//							}
						}while(data.moveToNext());
					}

					if(handle != null){
						handle.removeMessages(ENDDEAL);
						handle.sendEmptyMessage(ENDDEAL);
					}
				}
			}

			@Override
			public void onLoaderReset(Loader<Cursor> loader) {

			}
		});
	}


	/**删除数据库里面图片*/
	public  static void deletDbImage(Context c, String path){

		Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
		ContentResolver mContentResolver = c.getContentResolver();
		String where = MediaStore.Images.Media.DATA + "='" + path + "'";
		mContentResolver.delete(uri, where, null);
	}

	/**删除数据库里面图片*/
	public  static void deletDbVideo(Context c, String path){

		Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
		ContentResolver mContentResolver = c.getContentResolver();
		String where = MediaStore.Video.Media.DATA + "='" + path + "'";
		mContentResolver.delete(uri, where, null);
	}

}
